﻿using DeviceManager.ContentPage.UserMng;
using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage.SysMng
{
    /// <summary>
    /// DepmentView.xaml 的交互逻辑
    /// </summary>
    public partial class DepmentView : UserControl
    {
        /// <summary>
        /// 部门信息集合
        /// </summary>
        private List<DepmentInfo> depInfo = null;

        public DepmentView()
        {
            InitializeComponent();
            this.Loaded += DepmentView_Loaded;
        }

        private void DepmentView_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                RefData();
            }
        }
        

        void RefData()
        {
            List<DepmentInfo> infos = ManagerDBCtrl.GetMng().GetAllDepmentInfos();
            depInfo = new List<DepmentInfo>();
            if (infos.Count > 0)
            {
                if (infos[0].Chileds != null)
                {
                    foreach (var de in infos[0].Chileds)
                    {
                        depInfo.Add(de);
                    }
                }
            }

            depTree.ItemsSource = depInfo;
        }

        private void blt_new_Click(object sender, RoutedEventArgs e)
        {
            EditDepInfo edep = new EditDepInfo();
            edep.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (edep.ShowDialog() == true)
            {
                RefData();
            }
        }

        private void blt_edit_Click(object sender, RoutedEventArgs e)
        {
            if (depTree.SelectedItem == null)
            {
                MessageBox.Show("未选择编辑对象");
                return;
            }
            EditDepInfo edep = new EditDepInfo();
            edep.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            edep.SetEdit(depTree.SelectedItem as DepmentInfo);
            if (edep.ShowDialog() == true)
            {
                RefData();
            }
        }

        private void blt_del_Click(object sender, RoutedEventArgs e)
        {
            if (depTree.SelectedItem == null)
            {
                MessageBox.Show("未选择编辑对象");
                return;
            }
            if (depTree.SelectedItem is DepmentInfo)
            {
                DepmentInfo dep = depTree.SelectedItem as DepmentInfo;
                if (MessageBox.Show("您确定删除[" + dep.Name + "]组织机构信息？", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    if (ManagerDBCtrl.GetMng().DeleteDepMenteInfo(dep.Id))
                    {
                        RefData();
                    }
                    else
                    {
                        MessageBox.Show("删除信息失败");
                    }
                }
            }
        }

        private void depTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (depTree.SelectedItem is DepmentInfo)
            {
                DepmentInfo info = depTree.SelectedItem as DepmentInfo;
                if (info != null)
                {
                    this.tb_depDes.Text = info.Des;
                    this.tb_depName.Text = info.Name;
                    DepmentInfo parentDep = ManagerDBCtrl.GetMng().GetDepmentInfoByID(info.pid);
                    tb_parentdep.Tag = parentDep;
                    if (parentDep != null)
                    {
                        tb_parentdep.Text = parentDep.Name;
                        tb_parentdep.ToolTip = parentDep.Des;
                    }
                    this.Tag = depInfo;
                }
            }
        }
    }
}
